<template>
  <div class="bg-white" style="height: 100%">
    <div class="solid-bottom padding">
      <div class="bg-gray flex padding-sm text-lg">
        <text class="cuIcon-search"></text><input  placeholder=" 输入员工姓名或ID" v-model="keyWord"/>
      </div>

    </div>
    <div class="text-center solid-bottom padding margin-left margin-right-sm" @tap="addEmployee">
      <text class="cuIcon-add text-bold text-xxl" style="color: #313131">添加员工</text>
    </div>
    <div class="cu-list">
      <div class="cu-item solid-bottom padding-sm padding-top-xs margin-left-xs margin-right-xs " v-for="employee in filList" :key="employee.id" @click="onClick(employee)">
        <div class="content flex align-center">
          <div class="flex-sub">
            <div class="text-lg padding-bottom-xs" style="color: #2f2f2f">{{employee.name}}</div>
            <div class="text-sm">工号:{{employee.id}}</div>
          </div>
          <div class="flex-sub">
            <div v-if="adminId!==employee.id" class="cu-tag" v-for="auth in authMap[employee.auth]" :key="auth">{{auth}}</div>
            <div v-else class="cu-tag">超级管理员</div>
          </div>
          <div class="flex-sub text-right">
            <text v-if="adminId!==employee.id" class="cuIcon-right lg text-gray"></text>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import request from "../../utils/request";

export default {
  name: "index",
  data(){
    return{
      keyWord:"",
      adminId:uni.getStorageSync('userMain').id,
      employeeList:[],
      authMap:{
        0:["管理员"],
        1:["收银员","仓管员"],
        2:["仓管员"],
        3:["收银员"]
      }
    }
  },
  onLoad(){
    uni.setNavigationBarTitle({
      title: '员工管理'
    });
    request.get("/store/get/employee/"+uni.getStorageSync("currentStoreMain").storeId).then(res=>{
      this.employeeList=res.data
      this.employeeList.sort(function (a, b) {
        return a.auth-b.auth
      })
    })
  },
  onShow(){
    uni.$once('employeeUpdate', (data)=> {
      if (data){
        request.get("/store/get/employee/"+uni.getStorageSync("currentStoreMain").storeId).then(res=>{
          this.employeeList=res.data
          this.employeeList.sort(function (a, b) {
            return a.auth-b.auth
          })
        })
      }
    })
  },
  computed:{
    filList(){
      // 返回过滤后的数组
      return this.employeeList.filter((p)=>{
        return p.name.indexOf(this.keyWord) !==-1 || p.id.indexOf(this.keyWord) !==-1
      })
    }
  },
  methods:{
    onClick(employee){
      if(employee.id!==uni.getStorageSync('userMain').id)
      uni.navigateTo({
        url:"/pages/employee/EmployeeDetail",
        success:(res)=>{
          res.eventChannel.emit('employee',employee )
        }
      }
      )
    },
    otherFun(option){
      if (option==="update"){
        request.get("/store/get/employee/"+uni.getStorageSync("currentStoreMain").storeId).then(res=>{
          this.employeeList=res.data
          this.employeeList.sort(function (a, b) {
            return a.auth-b.auth
          })
        })
      }
    },
    addEmployee(){
      uni.navigateTo({
            url:"/pages/employee/AddEmployee"
          }
      )
    }
  }
}
</script>

<style scoped>

</style>